<?php
namespace Admin\Controller;
use Think\Controller;
	class RbacController extends InitializeController{
		public function switchRorA(){
			session_start();
			$_SESSION['RorA'] = 'Auth';
			if ($_SESSION['RorA'] == 'Auth') {
				$arr['success']=1;
				$arr['count'] = I('count');
				$arr['carCount'] = $this->carCount();
				echo json_encode($arr);
			}

		}

		public function role(){
			$this->role = M('Role')->select();
			$this->display();
		}

		public function addRole(){
			if (I('submit')=='保存添加') {
				if (M('Role')->add($_POST)) {
					$this->success('添加成功!',U('Rbac/role'));
				}else{
					$this->error('添加失败!');
				}
			}else{
				$this->display();
			}
		}

		public function addNode(){
			$type = I('get.type');
			switch ($type) {
				case 'app':
					$this->type = '应用';
					break;

				case 'con':
					$this->type = '控制器';
					break;

				case 'fun':
					$this->type = '方法';
					break;
			}

			if (I('submit')=='保存添加') {
				//sort字段构思不出用法 暂时取消添加sort
				switch (I('type')) {
					case 'app':
						$_POST['level'] = 1;
						$_POST['pid'] = 0;
						break;

					case 'con':
						$_POST['level'] = 2;
						$_POST['pid'] = I('pid');
						break;

					case 'fun':
						$_POST['level'] = 3;
						$_POST['pid'] = I('pid');
						break;
				}
				if (M('Node')->add($_POST)) {
					$this->success('添加成功!',U('Rbac/node'));
				}else{
					$this->error('添加失败!');
				}

			}else{
				$this->display();
			}
		}

		public function node(){
			$User=D('menu');
	        $this->dfs = $User->DFS('node');
			// test($this->$dfs);
			$this->display();
		}

		public function permission(){
			$User=D('Menu');
			$db = M('Access');
	        $dfs = $User->DFS('Node');
	        $acc = $db->where("role_id=%d",I('get.id'))->select();

	        foreach ($acc as $av) {
	        	foreach ($dfs as $dk => $dv) {
	        		if ($dv['id'] == $av['node_id']) {
	        			$dfs["$dk"]['checked'] = 1;
	        		}
	        	}
	        }
	  //       test($dfs);
			// die();
	        $this->assign('dfs',$dfs);
			$this->display();
		}

		public function setAccess(){
			$db = M('Access');
			$judge = $db->where("role_id=%d",I('role_id'))->delete();
			foreach ($_POST as $pk => $pv) {
				switch ($pk) {
					case 'app':
						$level = 1;
						foreach ($pv as $v) {
							$data[] = array(
								'id' => I('id'),
								'role_id'=> I('role_id'),
								'node_id' => $v,
								'level' => $level,
							);
						}
						break;

					case 'con':
						$level = 2;
						foreach ($pv as $v) {
							$data[] = array(
								'id' => I('id'),
								'role_id'=> I('role_id'),
								'node_id' => $v,
								'level' => $level,
							);
						}
						break;

					case 'fun':
						$level = 3;
						foreach ($pv as $v) {
							$data[] = array(
								'id' => I('id'),
								'role_id'=> I('role_id'),
								'node_id' => $v,
								'level' => $level,
							);
						}
						break;
				}
			}

			if ($judge = $db->addAll($data)) {
				$this->success('配置成功!',U('Rbac/role'));
			}

		}

		public function delete($id,$type=null){
			$db = M('Node');
			$getType = !isset($getType) ? $type : I('get.type');
			switch ($getType) {
				case 'app':
					$childId = $db->where("pid=%d",$id)->field('id')->select();
					foreach ($childId as $k => $v) {
						$this->delete($v['id'],'con');
					}
					$db->where("id=%d",$id)->delete();
					break;
				case 'con':
					$childId = $db->where("pid=%d",$id)->field('id')->select();
					foreach ($childId as $k => $v) {
						$this->delete($v['id'],'fun');
					}
					$db->where("id=%d",$id)->delete();
					break;
				case 'fun':
					if ($judge = $db->where("id=%d",$id)->delete()) {
						$this->redirect('Rbac/node');
					}
					break;
			}
		}

		public function update($id=null,$type=null){
			$db = M('Node');
			if (I('submit') == '保存修改') {
				$data = array(
					'name' => I('name'),
					'remark' => I('remark'),
					'status' => I('status'),
					);
				if ($judge = $db->where("id=%d",I('id'))->save($data)) {
					$this->success('修改成功!',U('Rbac/node'));
				}
			}else{
				$result = $db->where("id=%d",$id)->field('remark,name,status')->select();
				$this->assign('remark',$result['0']['remark']);
				$this->assign('name',$result['0']['name']);
				$this->assign('status',$result['0']['status']);
				$this->assign('type',$type);
				$this->display();
			}
		}
	}
 ?>
